<style lang="scss" scoped>

@import "../_init.scss";
@import "../_form.scss";

</style>

<template lang="pug">
.VUE
    form(@submit.prevent="submit" ref="form")
        .P
            input(name="account" v-model.trim="account" placeholder="请输入 邮箱 / 手机" spellcheck="false" :disabled="disabled" autocomplete="off" required)
            label.I.I-user
        .resend
            .P(v-if="mail")
                p 重置密码邮件已发送
                p 请到
                    a(:href="mail_link") {{account.split('@').pop().toUpperCase()}}
                    | 查收

            p(v-if="disabled")
                a.button.disabled
                    | 没收到？
                    span.second 
                        | {{resend}}
                        span 秒
                    | 后点此重发  
        p(v-if="!disabled")
            button(type="submit") 重置密码
        p
            a.gray.button(href="/auth/login") 返回登录

</template>

<script lang="coffee">
###
disabled
    0
    mail
    phone

    密码重置邮件已发送

    没收到？60秒后，点此重发
###
captcha = require '../_captcha'

TABLE = {
    MAIL : 1
    PHONE : 2
}
_timer = 0
store = require 'store'

export default {
    data:->
        {
            account : @$route.params.account or store.get('account') or ''
            resend:0
            mail:false
            phone:false
        }
    
    computed:
        disabled: ->
            @resend > 0
        mail_link:->
            require("~coffee/_lib/mail_link") @account

    mounted:->
        form = $(@$refs.form)
        form.find('input').focus().select()
        return

    methods:
        submit:->
            if @disabled
                return
            clearInterval _timer
            form = $(@$refs.form)
            account = @account.trim()

            captcha(
                form
                "*auth/password/reset"
                {
                    account
                    host:location.host
                }
                (res)=>
                    switch res
                        when TABLE.MAIL
                            @mail = account
                            @phone = false
                        when TABLE.PHONE
                            @phone = account
                            @mail = false
                    @resend = 60
                    _timer = setInterval(
                        =>
                            -- @resend
                            if @resend <= 0
                                clearInterval _timer
                        1000
                    )
                (err)=>
                    require('../_err/account') err, account
            )
}
</script>

